#!/bin/bash

# Test vdi backup and restore

. ./common

for i in `seq 0 5`; do
    _start_sheep $i
done

_wait_for_sheep 6

_cluster_format -c 6
_vdi_create test 12M

# create the first object
echo 0 | $DOG vdi write test 0 512
$DOG vdi snapshot test -s snap1

# create the second object
echo 1 | $DOG vdi write test $((4 * 1024 * 1024)) 512
$DOG vdi snapshot test -s snap2

# update the first object
echo 2 | $DOG vdi write test 0 512
$DOG vdi snapshot test -s snap3

# check vdis
_vdi_list
$DOG vdi tree | _filter_short_date
for i in `seq 1 3`; do
    $DOG vdi read test -s snap$i | md5sum
done

# create backup files between snapshots
$DOG vdi backup test -F snap1 -s snap2 > $STORE/backup.1.2
$DOG vdi backup test -F snap1 -s snap3 > $STORE/backup.1.3
$DOG vdi backup test -F snap2 -s snap3 > $STORE/backup.2.3

# restore backups
$DOG vdi restore test -s snap1 < $STORE/backup.1.2
_vdi_list
$DOG vdi tree | _filter_short_date

$DOG vdi restore test -s 4 < $STORE/backup.2.3
_vdi_list
$DOG vdi tree | _filter_short_date

$DOG vdi restore test -s snap1 < $STORE/backup.1.3
_vdi_list
$DOG vdi tree | _filter_short_date

$DOG vdi restore test -s snap2 < $STORE/backup.2.3
_vdi_list
$DOG vdi tree | _filter_short_date

# check vdi contents
$DOG vdi read test | md5sum
for i in `seq 1 $($DOG vdi list | grep "^s " | wc -l)`; do
    $DOG vdi read test -s $i | md5sum
done

# restore to non-existence snapshot, this must be error
$DOG vdi restore test -s non-existence-tag < $STORE/backup.2.3

true
